package com.example.stujava.module.socked.tcp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        String serverIP = "127.0.0.1";
        int serverPort = 10001;
        Socket socket = null;
        FileInputStream fis = null;
        try {
            //三次握手确认连接成功
            socket = new Socket(InetAddress.getByName(serverIP), serverPort);
            //获取流 (发送方-输出流)
            OutputStream os = socket.getOutputStream();
            //通过流传输数据
            File file = new File("D:\\file\\output\\o.jpg");
            fis = new FileInputStream(file);
            byte[] bytes = new byte[20 * 1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                os.write(bytes, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (socket != null) {
                try {
                    socket.shutdownOutput();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}
